package com.uuah.server.message.busi;

import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import com.uuah.server.api.message.IUuahBusiMessage;
import com.uuah.server.api.message.IUuahBusiMessageCollection;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:55:29
 * @version 1.0.0
 */
@SuppressWarnings("unchecked")
public class UuuahBusiMessageCollection implements IUuahBusiMessageCollection {
	/**
	 *
	 */
	private static final long serialVersionUID = 3051674333408120884L;
	private List<IUuahBusiMessage> messageList = new CopyOnWriteArrayList();
	private int totalLength = 0;

	public List<IUuahBusiMessage> getMessageList() {
		return messageList;
	}

	public void setMessageList(List<IUuahBusiMessage> messageList) {
		this.messageList = messageList;
	}

	public void addMessage(IUuahBusiMessage message) {
		getMessageList().add(message);
		totalLength += message.getLength();
	}

	public void addMessage(IUuahBusiMessage message, int index) {
		getMessageList().add(index, message);
		totalLength += message.getLength();
	}

	public void addMessages(IUuahBusiMessage[] messages) {
		for (int i = 0; i < messages.length; i++) {
			addMessage(messages[i]);
		}
	}

	public void addMessages(List<IUuahBusiMessage> messages) {
		for (Iterator iterator = messages.iterator(); iterator.hasNext();) {
			UuahBusiMessage message = (UuahBusiMessage) iterator.next();
			addMessage(message);
		}
	}

	public IUuahBusiMessage getMessage(int index) {
		return (UuahBusiMessage) getMessageList().get(index);
	}

	public IUuahBusiMessage[] getMessagesAsArray() {
		List list = getMessageList();
		IUuahBusiMessage[] messages = new UuahBusiMessage[list.size()];
		messages = (IUuahBusiMessage[]) list.toArray(messages);
		return messages;
	}

	public void removedMessage(IUuahBusiMessage message) {
		getMessageList().remove(message);
		totalLength -= message.getLength();
	}

	public int size() {
		return getMessageList().size();
	}

	public int totalLength() {
		return totalLength;
	}

}
